//
//  NewsViewController.m
//  HelpThePoor
//
//  Created by van7ish on 2016/12/2.
//  Copyright © 2016年 van7ish. All rights reserved.
//

#import "NewsViewController.h"
#import "NewsTableViewCell.h"
#import "NewsDetailViewController.h"
#import "NewsDetailVC.h"

@interface NewsViewController () <UITableViewDelegate, UITableViewDataSource>

/** 导航条 */
@property (nonatomic, strong) UIImageView * navBar;
/** 资讯列表 */
@property (nonatomic, strong) UITableView * newsTableView;
/** 数据源 */
@property (nonatomic, strong) NSMutableArray * newsArr;
/** 加载页数 */
@property (nonatomic, assign) NSInteger pageIndex;


@end

@implementation NewsViewController

- (NSInteger)pageIndex{
    if (!_pageIndex) {
        _pageIndex = 1;
    }
    return _pageIndex;
}

- (UIImageView *)navBar{
    if (!_navBar) {
        _navBar = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_W, 64)];
        _navBar.image = ImageNamed(@"daohangtiao");
    }
    return _navBar;
}

- (NSMutableArray *)newsArr{
    if (!_newsArr) {
        _newsArr = [NSMutableArray array];
    }
    return _newsArr;
}

- (UITableView *)newsTableView{
    if (!_newsTableView) {
        _newsTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_W, SCREEN_H - 64)];
        _newsTableView.delegate = self;
        _newsTableView.dataSource = self;
        _newsTableView.backgroundColor = COLOR_LIGHTBGCOLOR;
        _newsTableView.rowHeight = (SCREEN_W * 0.905 - WidthScale(44)) / 3;
        _newsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _newsTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _newsTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestCountyNews:)];
        _newsTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestCountyNews:)];

    }
    return _newsTableView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"热点资讯";
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    [self.view addSubview:self.navBar];
    
    [self.view addSubview:self.newsTableView];
    
    [self.newsTableView.mj_header beginRefreshing];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc{
    DLog(@"%s", __func__);
}

#pragma mark - UITableViewDelegate & UITableViewDataSource Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.newsArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellID = @"newsCell";
    NewsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (cell == nil) {
        cell = [[NewsTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }

    cell.titleLabel.text = [NSString stringWithFormat:@"%@", self.newsArr[indexPath.row][@"title"]];

    cell.timeLabel.text = [NSString stringWithFormat:@"%@", self.newsArr[indexPath.row][@"publishTime"]];
    
    
    NSString *imagePath = [NSString stringWithFormat:@"%@%@", KHostAddress, self.newsArr[indexPath.row][@"imagePath"]];
    [cell.coverImageView sd_setImageWithURL:[NSURL URLWithString:imagePath] placeholderImage:nil];

    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NewsDetailVC *newsDetailVC = [[NewsDetailVC alloc] init];
    newsDetailVC.dataDic = self.newsArr[indexPath.row];
    [self.navigationController pushViewController:newsDetailVC animated:YES];
//    NewsDetailViewController *newsDetailVC = [[NewsDetailViewController alloc] init];
//    newsDetailVC.dataDic = self.newsArr[indexPath.row];
//    [self.navigationController pushViewController:newsDetailVC animated:YES];
}

#pragma mark - data request

- (void)requestCountyNews:(UIView *)view{
    if ([view isKindOfClass:[MJRefreshNormalHeader class]]) {
        self.pageIndex = 1;
        [self.newsArr removeAllObjects];
    } else {
        self.pageIndex += 1;
    }
    NSString *URL = [NSString stringWithFormat:KNEWSURL, KHostAddress, @(self.pageIndex), KOrganizationID];
    [ZRRequestManager POST:URL parameters:nil success:^(id responseObject) {
        DLog(@"%@", responseObject);
        if ([[responseObject objectForKey:@"error"] integerValue] == 0) {
            
            [self.newsArr addObjectsFromArray:responseObject[@"data"][@"items"]];
            [self.newsTableView reloadData];
        }
        
        [self.newsTableView.mj_header endRefreshing];
        [self.newsTableView.mj_footer endRefreshing];
    } failure:^(NSError *error) {
        DLog(@"%@", error);
        [self.newsTableView.mj_header endRefreshing];
        [self.newsTableView.mj_footer endRefreshing];
    }];
}

@end
